package com.example.shoppingdemo.goods.GoodsJieXi;


import com.example.shoppingdemo.BaseApplication;
import com.example.shoppingdemo.goods.Bean.Addbuycar;
import com.example.shoppingdemo.goods.Bean.CommodityBeanOne;
import com.example.shoppingdemo.goods.Bean.CommodityBeanThree;
import com.example.shoppingdemo.goods.Bean.CommodityBeanTwo;
import com.example.shoppingdemo.goods.goodsJieKou.Goods;
import com.example.shoppingdemo.goods.model.BuycarModel;
import com.example.shoppingdemo.goods.model.GoodsModel;
import com.example.shoppingdemo.goods.model.SkuModel;

import java.util.ArrayList;
import java.util.List;

import okhttp3.FormBody;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class Goodsjiexi implements Goods {

    List<GoodsModel> list =new ArrayList<>();
    SkuModel skuModel = new SkuModel();
    List<BuycarModel> addlist = new ArrayList<>();
    @Override
    public void FindGoodsResquest(int id,ongoodslisetener ongoodslisetener) {

        BaseApplication.ishoppingRequest.requestFindKuoZhanList(id)
                .map(new Func1<CommodityBeanOne, CommodityBeanTwo>() {
                    @Override
                    public CommodityBeanTwo call(CommodityBeanOne commodityBeanOne) {
                        return commodityBeanOne.getData();
                    }
                }).map(new Func1<CommodityBeanTwo, CommodityBeanThree>() {
            @Override
            public CommodityBeanThree call(CommodityBeanTwo commodityBeanTwo) {
                String[] id = new String[commodityBeanTwo.getSkuStockList().length];
                String[] price = new String[commodityBeanTwo.getSkuStockList().length];
                String[] spData = new String[commodityBeanTwo.getSkuStockList().length];
                for (int i = 0; i < commodityBeanTwo.getSkuStockList().length; i++) {
                    id[i] = String.valueOf(commodityBeanTwo.getSkuStockList()[i].getId());
                    price[i] = String.valueOf(commodityBeanTwo.getSkuStockList()[i].getPrice());
                    spData[i] = commodityBeanTwo.getSkuStockList()[i].getSpData();
                }
                skuModel.setId(id);
                skuModel.setPrice(price);
                skuModel.setSpData(spData);
                return commodityBeanTwo.getPmsProduct();
            }
        }).map(new Func1<CommodityBeanThree, GoodsModel>() {
            @Override
            public GoodsModel call(CommodityBeanThree commodityBeanThree) {
                GoodsModel goodsModel = new GoodsModel();
                goodsModel.setPic(commodityBeanThree.getPic());
                goodsModel.setPrice(commodityBeanThree.getPrice());
                goodsModel.setName(commodityBeanThree.getName());
                goodsModel.setBrandName(commodityBeanThree.getBrandName());
                goodsModel.setAlbumPics(commodityBeanThree.getAlbumPics());
                return goodsModel;
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<GoodsModel>() {
                    @Override
                    public void onCompleted() {
                        if (ongoodslisetener != null){
                            ongoodslisetener.ongoodsmsg(list, skuModel);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(GoodsModel goodsModel) {
                        list.add(goodsModel);
                    }
                });

    }

    @Override
    public void AddGoodsResquest(int memberId, int productId, int productSkuId, int quantity,onAddcar car) {
        FormBody formBody = new FormBody.Builder()
                .add("memberId", String.valueOf(memberId))
                .add("productId", String.valueOf(productId))
                .add("productSkuId", String.valueOf(productSkuId))
                .add("quantity", String.valueOf(quantity))
                .build();
        BaseApplication.ishoppingRequest.requestAddList(formBody)
                .map(new Func1<Addbuycar, BuycarModel>() {
                    @Override
                    public BuycarModel call(Addbuycar addbuycar) {
                        BuycarModel buycarModel = new BuycarModel();
                        buycarModel.setMsg(addbuycar.getMessage());
                        return buycarModel;
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BuycarModel>() {
                    @Override
                    public void onCompleted() {
                        car.onAddmsg(addlist);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(BuycarModel buycarModel) {
                        addlist.add(buycarModel);
                    }
                });
    }

    public interface onAddcar{
        void onAddmsg(List<BuycarModel> list);
    }

    public interface ongoodslisetener{
        void ongoodsmsg(List<GoodsModel> list,SkuModel skuModel);
    }
}
